// commonMaterials


import {createSlice} from "@reduxjs/toolkit";
import {
    getCommonMaterialsCountAPI,
    getCommonMaterialsCountByNameAPI,
    getCommonMaterialsInfoAPI,
    getCommonMaterialsListAPI,
    getCommonMaterialsListByNameAPI
} from "@/apis/commonMaterials.jsx";


const commonMaterialsStore=createSlice({
    name:'commonMaterials',
    initialState:{
        commonMaterialsInfo_init:{},
        commonMaterialsList_init:[],
        commonMaterialsCount_init:0,
        commonMaterialsCur_init:1,
        commonMaterialsPageSize_init:11,
        commonMaterialsDriveListChange_init:0
    },
    reducers:{
        getCommonMaterialsInfo(state,action){
            state.commonMaterialsInfo_init=action.payload
        },
        getCommonMaterialsList(state,action){
            state.commonMaterialsList_init=action.payload
        },
        getCommonMaterialsCount(state,action){
            state.commonMaterialsCount_init=action.payload
        },
        setCommonMaterialsCur(state,action){
            state.commonMaterialsCur_init=action.payload;
        },
        setCommonMaterialsPageSize(state,action){
            state.commonMaterialsPageSize_init=action.payload;
        },
        setCommonMaterialsDriveListChange(state,action){
            state.commonMaterialsDriveListChange_init=action.payload;
        }
    }
})
const {
    setCommonMaterialsDriveListChange,
    getCommonMaterialsInfo,
    getCommonMaterialsList,
    getCommonMaterialsCount,
    setCommonMaterialsCur,
    setCommonMaterialsPageSize}=commonMaterialsStore.actions;

// 异步请求函数
// 分页获取所有数据
const fetchGetCommonMaterialsList=(cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getCommonMaterialsListAPI(cur,pageSize);
        dispatch(getCommonMaterialsList(res.data))
    }
}
const fetchGetCommonMaterialsInfo=(id)=>{
    return async (dispatch)=>{
        const res=await getCommonMaterialsInfoAPI(id);
        dispatch(getCommonMaterialsInfo(res.data))
    }
}
const fetchGetCommonMaterialsCount=()=>{
    return async (dispatch)=>{
        const res=await getCommonMaterialsCountAPI();
        dispatch(getCommonMaterialsCount(res.data))
    }
}

// 根据项目名称查询项目
const fetchGetCommonMaterialsListByName=(name,cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getCommonMaterialsListByNameAPI(name,cur,pageSize);
        console.log(res.data);
        dispatch(getCommonMaterialsList(res.data));

    }
}
const fetchGetCommonMaterialsCountByName=(name)=>{
    return async (dispatch)=>{
        const res=await getCommonMaterialsCountByNameAPI(name);
        console.log(res.data);
        dispatch(getCommonMaterialsCount(res.data))
    }
}

const commonMaterialsReducer=commonMaterialsStore.reducer;
export {
    fetchGetCommonMaterialsList,
    fetchGetCommonMaterialsInfo,
    fetchGetCommonMaterialsCount,
    fetchGetCommonMaterialsListByName,
    fetchGetCommonMaterialsCountByName,
    setCommonMaterialsCur,
    setCommonMaterialsPageSize,
    setCommonMaterialsDriveListChange
}

export default commonMaterialsReducer;